﻿using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using NetTaste;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.Data;
using System.Linq.Expressions;
using System.Numerics;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Text.RegularExpressions;
using VM.Common.DataUntil;

using VM.Common.Result;
using VM.Entity.EntityMap.DataBase;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.MsgCode;
using VM.Entity.EntityMap.Region;
using VM.Entity.EntityMap.Token;
using VM.Entity.EntityMap.Topic;
using VM.IBLL.DataBase;
using VM.IBLL.Member;
using VM.IBLL.Token;
using VM.IDAL;
using static Dm.net.buffer.ByteArrayBuffer;

namespace VM.BLL.DataBase
{
    public class DataBaseBLL : BaseBLL, IDataBaseBLL
    {

        private readonly ISqlSuggarDAL _ISqlSuggarDAL;
        public DataBaseBLL(ISqlSugarClient client, ISqlSuggarDAL sqlSuggarDAL)
        {
            _ISqlSuggarDAL = sqlSuggarDAL;
        }




        public ApiDataResult<sys_dataBaseType> addDataBaseType(sys_dataBaseType obj)
        {
            ApiDataResult<sys_dataBaseType> res = new ApiDataResult<sys_dataBaseType>();
            sys_dataBaseType topic = _ISqlSuggarDAL.Insert(obj);
            if (topic.id > 0)
            {
                res.code = 200;
                res.msg = "添加成功";
                res.data = topic;

            }
            else
            {
                res.code = 403;
                res.msg = "添加失败";
                res.data = null;


            }
            return res;
        }


        public ApiDataResult<PagingData<sys_dataBaseType>> getDataBaseTypeList(string? typeName, int pageSize, int pageIndex)
        {
            ApiDataResult<PagingData<sys_dataBaseType>> res = new ApiDataResult<PagingData<sys_dataBaseType>>();
            Expression<Func<sys_dataBaseType, bool>> funcWhere = null;

            if (typeName.Length > 0)
            {
                funcWhere = x => SqlFunc.Like(x.name, $"%{typeName}%");
            }
            Expression<Func<sys_dataBaseType, object>> funcOrderb = null;
            PagingData<sys_dataBaseType> data = _ISqlSuggarDAL.QueryPage(funcWhere, pageSize, pageIndex, funcOrderb);
            if (data.recordCount > 0)
            {
                res.code = 200;
                res.msg = "获取成功";
                res.data = data;
            }
            else
            {
                res.code = 200;
                res.msg = "无数据";
                res.data = null;
            }
            return res;
        }

        public ApiDataResult<sys_dataBase> addDataBase(sys_dataBase obj)
        {
            ApiDataResult<sys_dataBase> res = new ApiDataResult<sys_dataBase>();
            sys_dataBase topic = _ISqlSuggarDAL.Insert(obj);
            if (topic.id > 0)
            {
                res.code = 200;
                res.msg = "添加成功";
                res.data = topic;



            }
            else
            {
                res.code = 403;
                res.msg = "添加失败";
                res.data = null;


            }
            return res;
        }


        public ApiDataResult<PagingData<sys_dataBase>> getDataBaseList(string? name, int? parrentId, int pageSize, int pageIndex)
        {
            ApiDataResult<PagingData<sys_dataBase>> res = new ApiDataResult<PagingData<sys_dataBase>>();
            Expression<Func<sys_dataBase, bool>> funcWhere = x => true; ;
            Expression<Func<sys_dataBase, bool>> funcWhere1 = y => true;

            if (parrentId!=0)
            {
                funcWhere = x => x.parentId==parrentId;
            }
             

            Expression<Func<sys_dataBase, object>> funcOrderb = null;
            PagingData<sys_dataBase> data = _ISqlSuggarDAL.QueryPage(funcWhere, pageSize, pageIndex, funcOrderb);
            if (data.recordCount > 0)
            {
                res.code = 200;
                res.msg = "获取成功";
                res.data = data;
            }
            else
            {
                res.code = 200;
                res.msg = "无数据";
                res.data = null;
            }
            return res;
        }


    }




}
